<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddcat">添加分类</el-button>
        </el-col>
      </el-row>

      <!--表格 -->
      <tree-table
        :show-row-hover="false"
        index-text="#"
        :data="catelist"
        :columns="columns"
        border
        :selection-type="false"
        :expand-type="false"
        show-index
      >
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color:lightgreen" v-if="scope.row.cat_deleted===false"></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level===0">标签一</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level===1">标签二</el-tag>
          <el-tag type="danger" v-else>标签三</el-tag>
        </template>
        <template slot="operation" slot-scope="scope">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[ 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>

      <!-- 添加分类 -->
      <el-dialog title="添加分类" :visible.sync="showDialogVisible" width="50%" @close="addDialogClose">
        <!-- 表单 -->
        <el-form ref="formRef" :rules="addFormRules" :model="formData" label-width="80px">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="formData.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <!-- 级联选择器 -->
            <!-- options 用来指定数据源 -->
            <!-- props用来指定数据对象 -->
            <el-cascader
              clearable
              :props="cascaderProps"
              v-model="selectKeys"
              :options="paramsCateList"
              @change="paramsCascaedChange"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      catelist: [],
      formData: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0
      },
      showDialogVisible: false,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "order"
        },
        {
          label: "操作",
          type: "template",
          template: "operation"
        }
      ],
      //   表单验证规则
      addFormRules: {
        cat_name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ]
      },
      //  父级分类列表
      paramsCateList: [],
      //   指定联级选择器的配置对象
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
        checkStrictly: true
      },
      selectKeys: []
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取列表失败");
      }
      this.catelist = res.data.result;
      //   为总数据条数赋值
      this.total = res.data.total;
    },
    // 分页
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },
    // 添加分类
    showAddcat() {
      this.getparamCateList();
      this.showDialogVisible = true;
    },

    async getparamCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取父级列表失败");
      }
      //   console.log(res.data);
      this.paramsCateList = res.data;
    },
    paramsCascaedChange() {
      console.log(this.selectKeys);
      // 如果selectKeys 数组中length大于0，证明选中的是父级分类
      // 反正为选中父级分类
      //   父级分类的id
      if (this.selectKeys.length > 0) {
        this.formData.cat_pid = this.selectKeys[this.selectKeys.length - 1];
        // 为当前分类等级赋值
        this.formData.cat_level = this.selectKeys.length;
        return;
      } else {
        this.formData.cat_pid = 0;
        this.formData.cat_level = 0;
      }
    },
    // 单击按钮添加分类
    addCate() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return this.$message.error("验证规则错误");
        const { data: res } = await this.$http.post(
          "categories",
          this.formData
        );
        if (res.meta.status !== 201) {
          return this.$message.error("添加分类失败");
        }
        this.$message.success("添加成功");
        this.getparamCateList();
        this.showDialogVisible = false;
      });
    },
    addDialogClose() {
      this.$refs.formRef.resetFields();
      this.selectKeys = [];
      this.formData.cat_level = 0;
      this.formData.cat_pid = 0;
    }
  }
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>